/* 
 * JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * $Id: Logger.java,v 1.4 2006/06/26 05:00:17 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.controller;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author BigLee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Logger {
	
	
	private static final int LOG_SIZE=100;
	private static String[] log = null;
	private static int nextLogEntry = 0;
	/**
	 * accessor for log file does lazy initialization
	 * @return
	 */
	private synchronized String[] getLog() {
		if(log==null) {
			log=new String[LOG_SIZE];
			for(int i=0; i<LOG_SIZE; i++) {
				log[i]="";
			}
			nextLogEntry=0;
		}
		return log;
	}
	/**
	 * reset log to empty state
	 */
	public synchronized void resetLog() {
		log=null;
	}
	public synchronized void log(String s) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("DDD:HH:mm:ss.SSS");
	    Date nowDate = new Date();
	    String dateString = dateFormat.format(nowDate);
		String[] log = getLog();
		log[nextLogEntry++]= dateString + " " + s;
		if(nextLogEntry>=LOG_SIZE) {
			nextLogEntry = 0;
		}
	}
	
	public synchronized void log(String s, Throwable e) {
		if(s.length()>0) {
			log(s);
		}
		StringWriter sw = new StringWriter();
		e.printStackTrace(new PrintWriter(sw));
		log(sw.toString());
	}
	
	public synchronized void log(Throwable e) {
		log("",e);
	}
	
	public synchronized String getLogString() {
		String[] log = getLog();
		StringBuffer sb = new StringBuffer();
		int nextOut = nextLogEntry;
		for(int i=0; i<LOG_SIZE; i++) {
			String s = log[nextOut++];
			if(nextOut>=LOG_SIZE) {
				nextOut=0;
			}
			if(s!=null&&s.trim().length()>0) {
				sb.append(s);
				sb.append('\n');
			}
		}
		return sb.toString();
	}
	/*  
	  private LogFileWriter logFileWriter;
	  private SimpleDateFormat dateFormat =
	            new SimpleDateFormat("DDD:HH:mm:ss.SSS");
	  
	  Logger(ActionServlet mamaServlet)throws IOException{
	    String logFileDirectory = 
	      mamaServlet.getServletContext().getRealPath("WEB-INF/logs/");
	    logFileWriter = new LogFileWriter(logFileDirectory);
	  }
	  
	  public String log(String report){
	    String dateString;
	    Date nowDate = new Date();
	    synchronized (dateFormat){
	      dateString = dateFormat.format(nowDate);
	    }
	    String reportString = dateString + " " + report;
	    logFileWriter.writeln(reportString);
	    return dateString;
	  }
	  public String log(String report, Throwable tooty){
	    if (report.length()>0) report += "\r\n";
	    StringWriter sw = new StringWriter();
	    tooty.printStackTrace(new PrintWriter(sw));
	    return log(report +  sw.toString());
	  }
	  public String log(Throwable tooty){
	    return log("", tooty);
	  }
	  
	  void close(){
	    logFileWriter.close();
	  }
	*/
	}
